﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Treely.Data.Model.Abstract;

namespace Treely.Data.Model
{
    public class DivisionQuestion : MathAnswerable, IAnswerable
    {
        protected List<int> Factors { get; set; }

        public void Generate(AnswerType answerType, int minimumAnswer, int maximumAnswer, int numberOfDivisors, int numberOfAnswerChoices = 0)
        {
            Subject = QuestionSubject.Division;
            minimumAnswer = Math.Max(1, minimumAnswer); //prevent div by zero
            maximumAnswer = Math.Max(1, maximumAnswer); //prevent div by zero

            Factors = new List<int>();
            AnswerType = answerType;
            for (var i = 0; i < (Math.Max(1, numberOfDivisors)); i++)
            {
                Factors.Add(RandomNumber.Next(minimumAnswer, maximumAnswer));
            }
            CorrectAnswer = RandomNumber.Next(minimumAnswer, maximumAnswer);
            if (AnswerType == AnswerType.MultipleChoice)
            {
                int falseAnswer = CorrectAnswer;
                if (numberOfAnswerChoices == 0) numberOfAnswerChoices = 2;
                NumericAnswers = new List<int>() { CorrectAnswer };
                for (var j = 0; j < numberOfAnswerChoices; j++)
                {
                    while (NumericAnswers.Any(x => x.Equals(falseAnswer)))
                    {
                        falseAnswer = RandomNumber.Next(minimumAnswer, maximumAnswer);
                    }
                    NumericAnswers.Add(falseAnswer);
                }
            }
        }

        public DivisionQuestion(AnswerType answerType, int minimumAnswer, int maximumAnswer, int numberOfSubtractors,
            int numberOfAnswerChoices = 0)
        {
            RandomNumber = new Random();
            Generate(answerType, minimumAnswer, maximumAnswer, numberOfSubtractors, numberOfAnswerChoices);
        }

        public DivisionQuestion(DifficultyLevel level, Random random)
        {
            RandomNumber = random;
            switch (level)
            {
                case DifficultyLevel.None:
                case DifficultyLevel.Preschool:
                    Generate(AnswerType.MultipleChoice, 0, 3, 1, 2);
                    return;

                case DifficultyLevel.Kindergarten:
                    Generate(AnswerType.MultipleChoice, 0, 5, 1, 2);
                    return;

                case DifficultyLevel.FirstGrade:
                    Generate(AnswerType.MultipleChoice, 1, 10, 1, 4);
                    return;

                case DifficultyLevel.SecondGrade:
                    if (RandomNumber.Next(0, 100) > 50)
                        Generate(AnswerType.MultipleChoice, 1, 12, 1);
                    else Generate(AnswerType.Text, 1, 6, 1);

                    return;

                case DifficultyLevel.ThirdGrade:
                    if (RandomNumber.Next(0, 100) > 50)
                        Generate(AnswerType.MultipleChoice, 1, 12, 1);
                    else Generate(AnswerType.Text, 1, 12, 2);
                    return;

                default:
                    Generate(AnswerType.Text, 100, 400, 2);
                    return;
            }
        }

        public override string ToString()
        {
            return FactorListQuestionText.Select(x => x.ToString()).Aggregate((x, y) => x.ToString() + " / " + y.ToString()) + " = ?";
        }

        public override string ToStringVertical()
        {
            return base.ToStringVertical('/', FactorListQuestionText);
        }

        protected List<int> FactorListQuestionText
        {
            get
            {
                var list = Factors.OrderByDescending(x => x).ToList();
                list.Insert(0, CorrectAnswer * list.Aggregate((x, y) => x * y));
                return list;
            }
        }
    }
}